package com.telecom.weixi.adapter;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.telecom.weixi.R;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class CustInfoAdapter extends BaseAdapter {
	public static final String Tag_Cust_Portrait = "cust_portait";	//Drawable
	public static final String Tag_Cust_Name = "cust_name";			//String
	public static final String Tag_Cust_Phone = "cust_phone";		//String
	public static final String Tag_Cust_Type = "cust_type";			//String
	public static final String Tag_Cust_Title = "cust_title";		//String
	public static final String Tag_Next_Arrow = "next_arrow";	//Drawable		
	List<Map<String, Object>> custInfo;
	LayoutInflater flater;
	
	public CustInfoAdapter(Context context){
		custInfo = new ArrayList<Map<String, Object>>();
		flater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}
	
	public void addItem(Map<String, Object> item){
		custInfo.add(item);
	}
	
	public void clearData(){
		custInfo.clear();
	}
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return custInfo.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return custInfo.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder holder;
		if(convertView != null){
			holder = (ViewHolder) convertView.getTag();
		}else{
			holder = new ViewHolder();
			convertView = flater.inflate(R.layout.item_cust_info, null);
			holder.portrait = (ImageView) convertView.findViewById(R.id.info_portrait);
			holder.container = (LinearLayout) convertView.findViewById(R.id.info_container);
			holder.name = (TextView) convertView.findViewById(R.id.info_name);
			holder.phone = (TextView) convertView.findViewById(R.id.info_phone);
			holder.type = (TextView) convertView.findViewById(R.id.info_type);
			holder.title = (TextView) convertView.findViewById(R.id.info_title);
			holder.nextArrow = (ImageView) convertView.findViewById(R.id.next_arrow);
			convertView.setTag(holder);
		}
		
		Map<String, Object> item = custInfo.get(position);
		Drawable portrait = (Drawable) item.get(Tag_Cust_Portrait);
		if(portrait != null){
			holder.portrait.setBackgroundDrawable(portrait);
		}
		holder.name.setText((CharSequence) item.get(Tag_Cust_Name));
		holder.phone.setText((CharSequence) item.get(Tag_Cust_Phone));
		holder.type.setText((CharSequence) item.get(Tag_Cust_Type));
		holder.title.setText((CharSequence) item.get(Tag_Cust_Title));
		Drawable nextArrow = (Drawable) item.get(Tag_Next_Arrow);
		if(nextArrow != null){
			holder.nextArrow.setBackgroundDrawable(nextArrow);
		}
		
		return convertView;
	}
	
	class ViewHolder{
		ImageView portrait;
		LinearLayout container;
		TextView name;
		TextView phone;
		TextView type;
		TextView title;
		ImageView nextArrow;
	}

}
